﻿Imports System.Data.OleDb

Public Class EditProfile
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim sql As String = "SELECT * FROM UserProfile WHERE Username= @f1"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            'Creating a data reader to read data
            Dim datareader = cmd.ExecuteReader()
            If datareader.HasRows Then
                datareader.Read()
                If Not IsDBNull(datareader("Title")) Then txtTitle.SelectedValue = datareader("Title")
                If Not IsDBNull(datareader("FirstName")) Then txtFirstName.Text = datareader("FirstName")
                If Not IsDBNull(datareader("LastName")) Then txtLastName.Text = datareader("LastName")
                If Not IsDBNull(datareader("DOB")) Then txtDOB.Text = datareader("DOB")
                If Not IsDBNull(datareader("Address1")) Then txtAddress1.Text = datareader("Address1")
                If Not IsDBNull(datareader("Address2")) Then txtAddress2.Text = datareader("Address2")
                If Not IsDBNull(datareader("Town")) Then txtTown.Text = datareader("Town")
                If Not IsDBNull(datareader("City")) Then txtCity.Text = datareader("City")
                If Not IsDBNull(datareader("Postcode")) Then txtPostCode.Text = datareader("Postcode")
                If Not IsDBNull(datareader("TelNo")) Then txtTelNo.Text = datareader("TelNo")
                If Not IsDBNull(datareader("Gender")) Then txtGender.SelectedValue = datareader("Gender")
                If Not IsDBNull(datareader("AboutMe")) Then txtAboutMe.Text = datareader("AboutMe")
                If Not IsDBNull(datareader("PreferredFuel")) Then txtPreferredFuel.SelectedValue = datareader("PreferredFuel")
                If Not IsDBNull(datareader("FavouriteColour")) Then txtFavouriteColour.Text = datareader("FavouriteColour")
                If Not IsDBNull(datareader("UserImage")) Then userImage.ImageUrl = datareader("UserImage")
            Else
                userImage.ImageUrl = "/Images/User.png"
            End If
            datareader.Close()
            cmd.Dispose()
            conn.Close()
            conn.Dispose()
        End If
    End Sub

    Protected Sub txtSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles txtSubmit.Click
        Dim newFileLoc As String = ""
        Dim sql As String
        'If txtImage.HasFile Then
        '    newFileLoc = Request.MapPath("Images/UserImages/")
        '    newFileLoc = newFileLoc & "/" & txtImage.FileName
        '    txtImage.SaveAs(newFileLoc)
        'Else
        '    Return
        'End If
        Dim myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() & ".jpg"
        Dim fileLocationOnDisk = Request.MapPath("Images/UserImages") & "/" & newFileName
        txtImage.SaveAs(fileLocationOnDisk)
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If txtImage.HasFile Then
            sql = "UPDATE UserProfile SET Title=@f1, FirstName=@f2, LastName=@f3, DOB=@f6, Address1=@f7,Address2=@f12, UserImage=@f8, City=@f10, Town=@f11,PostCode=@f13,TelNo=@f14,Gender=@f15,AboutMe=@f16,PreferredFuel=@f17,FavouriteColour=@f18 WHERE Username=@f9"
        Else
            sql = "UPDATE UserProfile SET Title=@f1, FirstName=@f2, LastName=@f3, DOB=@f6, Address1=@f7,Address2=@f12, City=@f10, Town=@f11,PostCode=@f13,TelNo=@f14,Gender=@f15,AboutMe=@f16,PreferredFuel=@f17,FavouriteColour=@f18   WHERE Username=@f9"
        End If
        Dim cmd As New OleDbCommand(sql, conn)
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", txtTitle.SelectedValue)
        cmd.Parameters.AddWithValue("@f2", txtFirstName.Text)
        cmd.Parameters.AddWithValue("@f3", txtLastName.Text)
        cmd.Parameters.AddWithValue("@f6", txtDOB.Text)
        cmd.Parameters.AddWithValue("@f7", txtAddress1.Text)
        cmd.Parameters.AddWithValue("@f12", txtAddress2.Text)
        If txtImage.HasFile Then cmd.Parameters.AddWithValue("@f8", "Images/UserImages/" & newFileName)
        cmd.Parameters.AddWithValue("@f10", txtCity.Text)
        cmd.Parameters.AddWithValue("@f11", txtTown.Text)
        cmd.Parameters.AddWithValue("@f13", txtPostCode.Text)
        cmd.Parameters.AddWithValue("@f14", txtTelNo.Text)
        cmd.Parameters.AddWithValue("@f15", txtGender.Text)
        cmd.Parameters.AddWithValue("@f16", txtAboutMe.Text)
        cmd.Parameters.AddWithValue("@f17", txtPreferredFuel.Text)
        cmd.Parameters.AddWithValue("@f18", txtFavouriteColour.Text)
        cmd.Parameters.AddWithValue("@f9", User.Identity.Name)

        cmd.ExecuteNonQuery()

        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("MyVolkswagen.aspx?Username=" & User.Identity.Name)

    End Sub


    'Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    '    Dim sql As String
    '    Dim myGUID = Guid.NewGuid()
    '    Dim newFileName As String = myGUID.ToString() & ".jpg"
    '    Dim fileLocationOnDisk = Request.MapPath("Images/UserImages") & "/" & newFileName
    '    txtImage.SaveAs(fileLocationOnDisk)

    '    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
    '    If txtImage.HasFile Then
    '        sql = "UPDATE UserProfile SET Title=@f2, FirstName=@f3, LastName=@f4, DOB=@f5, Address1=@f6, Address2=@f7, Town=@f8, City=@f9, Postcode=@f10, TelNo=@f11, Gender=@f12, AboutMe=@f13, PreferredFuel=@f14, FavouriteColour=@f15, UserImage=@f16 WHERE Username=@f1"
    '    Else
    '        sql = "UPDATE UserProfile SET Title=@f2, FirstName=@f3, LastName=@f4, DOB=@f5, Address1=@f6, Address2=@f7, Town=@f8, City=@f9, Postcode=@f10, TelNo=@f11, Gender=@f12, AboutMe=@f13, PreferredFuel=@f14, FavouriteColour=@f15 WHERE Username=@f1"

    '    End If

    '    Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
    '    conn.Open()
    '    cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
    '    cmd.Parameters.AddWithValue("@f2", txtTitle.SelectedValue)
    '    cmd.Parameters.AddWithValue("@f3", txtFirstName.Text)
    '    cmd.Parameters.AddWithValue("@f4", txtLastName.Text)
    '    cmd.Parameters.AddWithValue("@f5", txtDOB.Text)
    '    cmd.Parameters.AddWithValue("@f6", txtAddress1.Text)
    '    cmd.Parameters.AddWithValue("@f7", txtAddress2.Text)
    '    cmd.Parameters.AddWithValue("@f8", txtTown.Text)
    '    cmd.Parameters.AddWithValue("@f9", txtCity.Text)
    '    cmd.Parameters.AddWithValue("@f10", txtPostCode.Text)
    '    cmd.Parameters.AddWithValue("@f11", txtTelNo.Text)
    '    cmd.Parameters.AddWithValue("@f12", txtGender.SelectedValue)
    '    cmd.Parameters.AddWithValue("@f13", txtAboutMe.Text)
    '    cmd.Parameters.AddWithValue("@f14", txtPreferredFuel.SelectedValue)
    '    cmd.Parameters.AddWithValue("@f15", txtFavouriteColour.Text)
    '    cmd.Parameters.AddWithValue("@f16", "Images/UserImages/" & newFileName)
    '    cmd.ExecuteNonQuery()
    '    cmd.Dispose()
    '    conn.Close()
    '    conn.Dispose()
    '    Response.Redirect("MyVolkswagen.aspx")
    'End Sub
End Class